package com.hefeng.controller;

import com.hefeng.common.core.web.controller.BaseController;
import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.common.core.web.page.TableDataInfo;
import com.hefeng.domain.Unit;
import com.hefeng.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/unit")
public class UnitController extends BaseController {
    @Autowired
    private UnitService unitService;
//新增
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody Unit unit) {

        return toAjax(unitService.insertUnit(unit));

    }

    @PostMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String unitCode,
                                        @RequestParam String unitCreditCode, @RequestParam String unitDetachmentName) {
        int startIdx = (pageNum - 1) * pageSize, size = pageSize;
        List<Unit> data =unitService.selectPageWithParam(startIdx, size, unitCode, unitCreditCode, unitDetachmentName);//获取一页的数据
        int total = unitService.selectTotalWithParam(unitCode, unitCreditCode, unitDetachmentName);//查询总条数
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);//表格数据
        res.put("total", total);//分页使用
        return res;
    }

    /**
     * 获取所有列表
     * @param unit
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(Unit unit)
    {
        startPage();
        List<Unit> list = unitService.selestlist(unit);
        return getDataTable(list);
    }




}
